#!/bin/bash

# 脚本所在路径：/usr/local/snowfield/script/lb_restart.sh
# 重启或重载与负载均衡相关的服务
# 1.重启keepalived
# 2.重启或重载haproxy,如果haproxy进程未启动，则启动；如果已启动，则重载
#
# return 0:正常退出，1:重启keepalived异常，2:重启haproxy异常，3:重载haproxy异常
#

/sbin/service keepalived restart
if [ $? -ne 0 ]; then
    echo "restart keepalived error, please check."
    exit 1
fi

haPidCnt=$(ps ax | grep "/usr/sbin/haproxy" | grep -v grep | wc -l)
if [ $haPidCnt == 0 ];then
  systemctl start haproxy.service
  if [ $? -ne 0 ]; then
      echo "restart haproxy.service error, please check."
      exit 2
  fi
else
  systemctl reload haproxy.service
  if [ $? -ne 0 ]; then
      echo "reload haproxy.service error, please check."
      exit 3
  fi
fi
exit 0
